package zhang.automobileservice;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;

import org.json.JSONException;
import org.json.JSONObject;


import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import utils.MyUrl;
import utils.RequestQueueUtils;
import utils.ToastUtil;

public class FindPasswordActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private TextView tv_exaphone;
    private TextView tv_resPwd;
    private EditText input_phone;
    private EditText input_checkcode;
    private Button ib_codephone;
    private Button bt_unLogin;
    private LinearLayout LL_exa_phone;
    private LinearLayout LL_re_pwd;
    private EditText et_pwd;
    private ImageButton ib_visible;
    private ImageButton ib_invisible;
    private Button bt_submit;
    private String tel;
    private EventHandler eh;
    private TimeCount time;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ToastUtil.show(getBaseContext(), "获取验证码成功");
                    break;
                case 2:
                    LL_exa_phone.setVisibility(View.GONE);
                    LL_re_pwd.setVisibility(View.VISIBLE);
                    tv_resPwd.setTextColor(getResources().getColor(R.color.logintext));
                    break;
                case 3:
                    ToastUtil.show(getBaseContext(), "验证失败，请重试……");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        initView();
        time = new TimeCount(30000, 1000);
        eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        handler.sendEmptyMessage(2);
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        Log.e("data",data.toString());
                        handler.sendEmptyMessage(1);

                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                } else {
                    ((Throwable) data).printStackTrace();
                    Log.e("======", data.toString());
                    handler.sendEmptyMessage(3);
                }
            }
        };
        SMSSDK.registerEventHandler(eh); //注册回调监听接口
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        tv_exaphone = (TextView) findViewById(R.id.tv_exaphone);
        tv_resPwd = (TextView) findViewById(R.id.tv_resPwd);
        input_phone = (EditText) findViewById(R.id.input_phone);
        input_checkcode = (EditText) findViewById(R.id.input_checkcode);
        ib_codephone = (Button) findViewById(R.id.ib_codephone);
        bt_unLogin = (Button) findViewById(R.id.bt_unLogin);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        ib_visible = (ImageButton) findViewById(R.id.ib_visible);
        ib_invisible = (ImageButton) findViewById(R.id.ib_invisible);
        bt_submit = (Button) findViewById(R.id.bt_submit);

        finish.setOnClickListener(this);
        ib_codephone.setOnClickListener(this);
        bt_unLogin.setOnClickListener(this);
        ib_visible.setOnClickListener(this);
        ib_invisible.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        LL_exa_phone = (LinearLayout) findViewById(R.id.LL_exa_phone);
        LL_exa_phone.setOnClickListener(this);
        LL_re_pwd = (LinearLayout) findViewById(R.id.LL_re_pwd);
        LL_re_pwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.ib_codephone:
                tel = input_phone.getText().toString().trim();
                if (!"".equals(tel) && tel.length() == 11) {
                    time.start();// 开始计时
                    SMSSDK.getVerificationCode("86", tel);
                } else {
                    ToastUtil.show(getBaseContext(), "请输入正确的电话号码");
                }
                break;
            case R.id.bt_unLogin:
                String code = input_checkcode.getText().toString().trim();
                if(null==tel){
                    ToastUtil.show(getBaseContext(), "你输入的电话号码为空");
                } else if(tel.length() != 11){
                    ToastUtil.show(getBaseContext(), "请输入正确的电话号码");
                }else if (code.length() == 4&&tel.length() == 11) {
                    SMSSDK.submitVerificationCode("86", tel, code);
                } else {
                    ToastUtil.show(getBaseContext(), "请输入正确的验证码");
                }
                break;
            case R.id.ib_visible:

                break;
            case R.id.ib_invisible:

                break;
            case R.id.bt_submit:
                String password = et_pwd.getText().toString().trim();
                if(password.length()<6){
                    ToastUtil.show(getApplicationContext(),"你输入的密码小于6位");
                }else{
                    getData(password);
                }
                break;
        }
    }

    private void getData(String password) {

        String url =MyUrl.Font_Url+MyUrl.findPwd+"?phone = "+tel+"&password="+password;
        Log.e ("------------",url);
        RequestQueueUtils.exectueJsonRequest(getApplicationContext(), url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if("0".equals(jsonObject.getString("code"))){
                        ToastUtil.show(getApplicationContext(),"修改成功");
                        Log.e ("------------","修改成功");
                        ToastUtil.show(getApplicationContext(),"密码修改成功");
                        Intent intent = new Intent(getApplicationContext(),MyLoginActivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e ("------------",e.toString());
                }
            }
        });
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            ib_codephone.setText("获取验证码");
            ib_codephone.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            ib_codephone.setClickable(false);//防止重复点击
            ib_codephone.setText((int) (millisUntilFinished / 1000) + "s");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_exaphone.setTextColor(getResources().getColor(R.color.logintext));
        LL_re_pwd.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }

}
